#!/bin/bash
# JVM参数来源于https://juejin.cn/post/7109260648453767204
# 针对普通的可执行的jar文件，提供启动，停止，重启等功能
# 需要根据jar包名称更改APP_JAR
# ms 和 mx 也需要根据情况调整
# SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
APP_JAR="kepler-social-web-0.0.1-SNAPSHOT.jar"
APP_PATH="f:\apprun"
LOG_PATH="f:\apprun\logs"
JAVA_OPTS="
-XX:+UnlockDiagnosticVMOptions 
-XX:+UnlockExperimentalVMOptions 
-XX:-OmitStackTraceInFastThrow 
-Xlog:gc*=debug:file=${LOG_PATH}/gc.log:utctime,level,tags:filecount=50,filesize=100M 
-Xlog:jit+compilation=info:file=${LOG_PATH}/jit_compile%t.log:utctime,level,tags:filecount=10,filesize=10M 
-Xlog:safepoint=debug:file=${LOG_PATH}/safepoint%t.log:utctime,level,tags:filecount=10,filesize=10M 
-Dfile.encoding=UTF-8 
-Djava.security.egd=file:/dev/./urandom 
-Dnetworkaddress.cache.ttl=10 
-XX:MaxRAMPercentage=3
-XX:InitialRAMPercentage=6
-XX:MinHeapFreeRatio=0 
-XX:MaxHeapFreeRatio=100 
-XX:+AlwaysPreTouch 
-Xss512k 
-XX:MaxDirectMemorySize=1024m 
-XX:MaxMetaspaceSize=384m 
-XX:ReservedCodeCacheSize=256m 
-XX:+DisableExplicitGC 
-XX:MaxGCPauseMillis=50 
-XX:-UseBiasedLocking 
-XX:GuaranteedSafepointInterval=0 
-XX:+UseCountedLoopSafepoints 
-XX:+SafepointTimeout 
-XX:SafepointTimeoutDelay=1000 
-XX:StartFlightRecording=disk=true,maxsize=4096m,maxage=3d 
-XX:FlightRecorderOptions=maxchunksize=128m 
--add-opens java.base/java.lang=ALL-UNNAMED 
--add-opens java.base/java.io=ALL-UNNAMED 
--add-opens java.base/java.math=ALL-UNNAMED 
--add-opens java.base/java.net=ALL-UNNAMED 
--add-opens java.base/java.nio=ALL-UNNAMED 
--add-opens java.base/java.security=ALL-UNNAMED 
--add-opens java.base/java.text=ALL-UNNAMED 
--add-opens java.base/java.time=ALL-UNNAMED 
--add-opens java.base/java.util=ALL-UNNAMED 
--add-opens java.base/jdk.internal.access=ALL-UNNAMED 
--add-opens java.base/jdk.internal.misc=ALL-UNNAMED"

start(){
  echo "start"
  if [ ! -d "$LOG_PATH"  ];then
    echo '创建'
    mkdir "$LOG_PATH"
  fi
  #nohup 
  java -Dspring.profiles.active=dev -Dlog.dir=$LOG_PATH $JAVA_OPTS -jar $APP_PATH/$APP_JAR >$LOG_PATH/console.log 2>&1 & 
  echo "app $APP_PATH/$APP_JAR started."
  echo "JAVA_OPTS: $JAVA_OPTS"
}

stop(){
    # WINDOW环境无效，等上linux再说，起始不用APP_PATH也可以，因为没有谁会用
    ps -ef|grep $APP_PATH/$APP_JAR|grep -v grep|awk '{print $2}'|xargs kill -9
    echo "app $APP_PATH/$APP_JAR is killed."
}

restart(){
	stop
	start
}

echo "APP_HOME:$APP_PATH"
case $1 in
	"start")
		start
		;;
	"stop")
		stop
		;;
	"restart")
		restart
		;;
	*)
		echo "Usage: app.sh ( commands ... )"
		echo "commands:"
		echo "  start             Start java app"
		echo "  stop              Stop java app"
		echo "  restart           Restart java app"
		;;
esac
